package server.update;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.SQLException;
import java.util.ArrayList;

import server.dao.ServerDao;
import server.to.UpdateTo;
import server.utility.DateUtil;

public class UpdateService implements Runnable{
	
	private Socket updateSocket;
	private PrintWriter updateAnswer;
	private ServerDao serverDao;
	
	public UpdateService(Socket updateSocket){
		this.updateSocket = updateSocket;
	}

	public void run() {
		try{
			updateAnswer = new PrintWriter(updateSocket.getOutputStream());
			ArrayList<UpdateTo> list = serverDao.selectRecentUpdatePool(DateUtil.formatDateForUpdateSelect(DateUtil.getCurrentDate()));
			System.out.println("서버 DB에서 업데이트 정보 확인 : "+list);
			if(list.size()>0){
				updateAnswer.print("true");
				System.out.println("update 할거 있다고 보냄");
			}else{
				updateAnswer.print("false");
				System.out.println("update 할게 없음");
			}
		}catch(IOException ioe){
			ioe.printStackTrace();
		}catch(SQLException sqle){
			sqle.printStackTrace();
		}finally{
			try{
				if(updateAnswer!=null){
					updateAnswer.close();
				}
				if(updateSocket!=null){
					updateSocket.close();
				}
				System.out.println("모두 완료하고 종료함");
			}catch(IOException ioe2){
				ioe2.printStackTrace();
			}
		}
	}
}
